package com.czy.owner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.entity.GoodsEntity;
import com.czy.owner.utils.glide.GlideUtils;

import java.util.List;

/**
 * Created by asus on 2017/5/17.
 */
public class ShoppingCartAdapter extends BaseRecyclerAdapter<GoodsEntity, ShoppingCartAdapter.ShoppingCartViewHolder> {
    private boolean isPopShow;
    private boolean isCheckAll;
    private boolean isCheckAllClick;
    private Context context;
    private List<GoodsEntity> deleteData;
    private List<GoodsEntity> settlementData;
    private PopShowListener popShowListener;
    private GoodsSelectListener goodsSelectListener;
    private CheckAllListener checkAllListener;

    public ShoppingCartAdapter(Context context, List<GoodsEntity> data, List<GoodsEntity> deleteData, List<GoodsEntity> settlementData, boolean isPopShow, boolean isCheckAll, boolean isCheckAllClick) {
        super(context, data);
        this.context = context;
        this.deleteData = deleteData;
        this.settlementData = settlementData;
        this.isPopShow = isPopShow;
        this.isCheckAll = isCheckAll;
        this.isCheckAllClick = isCheckAllClick;
    }

    @Override
    public ShoppingCartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ShoppingCartViewHolder(layoutInflater.inflate(R.layout.item_shoppoing_cart_recycler, parent, false));

    }

    @Override
    public void onBindViewHolder(ShoppingCartViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position));
    }

    public class ShoppingCartViewHolder extends BaseRecyclerAdapter.ViewHolder {
        private CheckBox cbGoods;
        private ImageView ivGoodsImg;
        private TextView tvGoodsName;
        private TextView tvPriceGoods;
        private TextView tvGoodsSpec;
        private TextView tvCountGoods;
        private ImageView ivArrowGoods;
        private TextView tvMemberPrice;

        public ShoppingCartViewHolder(View itemView) {
            super(itemView);
            cbGoods = (CheckBox) itemView.findViewById(R.id.cb_goods);
            ivGoodsImg = (ImageView) itemView.findViewById(R.id.iv_goods_img);
            tvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tvPriceGoods = (TextView) itemView.findViewById(R.id.tv_price_goods);
            tvGoodsSpec = (TextView) itemView.findViewById(R.id.tv_goods_spec);
            tvCountGoods = (TextView) itemView.findViewById(R.id.tv_count_goods);
            ivArrowGoods = (ImageView) itemView.findViewById(R.id.iv_arrow_goods);
            tvMemberPrice = (TextView) itemView.findViewById(R.id.tv_member_price);
        }

        public void bindData(final GoodsEntity model) {
            //当前安装门店信息赋值
            /*InstallStoreBean store = model.getStore();
            if (store != null) {
                model.setServiceInstallStoreId(store.getStoreId());
                model.setServicePrice(store.getServicePrice());
                model.setInstallStoreName(store.getStoreName());
                model.setNeedInstall(true);
            }
            else {
                model.setNeedInstall(false);
            }*/
            GlideUtils.loadImage(context, model.getLogo(), ivGoodsImg, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
                @Override
                public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

                }

                @Override
                public void onLoadingError(String source, Exception e) {

                }
            }, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
            if (!TextUtils.isEmpty(model.getGoodsName()) && model.getGoodsName().length() > 15) {
                tvGoodsName.setText(model.getGoodsName().substring(0, 14) + "...");
            } else {
                tvGoodsName.setText(model.getGoodsName());
            }

            tvPriceGoods.setText(String.format("%.2f", model.getGoodsSpecValuesPrice()));
            tvCountGoods.setText("x" + model.getCount());
            if (model.isHaveMemberPriceFlag()) {
                tvMemberPrice.setVisibility(View.VISIBLE);
            } else {
                tvMemberPrice.setVisibility(View.GONE);
            }
            tvMemberPrice.setText("￥"+String.format("%.2f", model.getStoreMemberPrice()));
            if (TextUtils.isEmpty(model.getGoodsSpec()) || "规格:无规格".equals(model.getGoodsSpec())) {
                tvGoodsSpec.setText("");
            } else {
                tvGoodsSpec.setText(model.getGoodsSpec());
            }

            ivArrowGoods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (popShowListener != null)
                        popShowListener.popShow(model, v);

                }
            });
            cbGoods.setTag(model);
            cbGoods.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (((GoodsEntity) buttonView.getTag()).equals(model)) {
                        if (isPopShow) {
                            if (isChecked) {
                                if (!deleteData.contains(model)) {
                                    deleteData.add(model);
                                }
                            } else {
                                if (deleteData.contains(model)) {
                                    deleteData.remove(model);
                                    if (isCheckAll) {
                                        if (checkAllListener != null) {
                                            checkAllListener.checkAllChange(isPopShow);
                                        }
                                    }
                                }
                            }
                        } else {
                            if (isChecked) {
                                if (!settlementData.contains(model)) {
                                    settlementData.add(model);
                                }
                            } else {
                                if (settlementData.contains(model)) {
                                    settlementData.remove(model);
                                    if (isCheckAll) {
                                        if (checkAllListener != null) {
                                            checkAllListener.checkAllChange(isPopShow);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (goodsSelectListener != null)
                        goodsSelectListener.goodsSelect(settlementData, deleteData);
                }
            });
            if (isPopShow) {
                ivArrowGoods.setVisibility(View.VISIBLE);
                tvCountGoods.setVisibility(View.GONE);
                if (deleteData.contains(model)) {
                    cbGoods.setChecked(true);
                } else {
                    cbGoods.setChecked(false);
                }
            } else {
                ivArrowGoods.setVisibility(View.GONE);
                tvCountGoods.setVisibility(View.VISIBLE);
                if (settlementData.contains(model)) {
                    cbGoods.setChecked(true);
                } else {
                    cbGoods.setChecked(false);
                }
            }
            if (isCheckAllClick) {
                if (isCheckAll) {
                    cbGoods.setChecked(true);
                } else {
                    cbGoods.setChecked(false);
                }
            }
        }

    }


    public void switchPopShow(boolean isShown) {
        isPopShow = isShown;
        notifyDataSetChanged();
    }

    public void checkAll(boolean isCheck) {
        isCheckAllClick = true;
        isCheckAll = isCheck;
        notifyDataSetChanged();
    }

    public void switchCheckAll() {
        isCheckAllClick = false;
        notifyDataSetChanged();
    }

    public void setOnPopShowListener(PopShowListener popShowListener) {
        this.popShowListener = popShowListener;
    }

    public interface PopShowListener {
        void popShow(GoodsEntity model, View v);
    }

    public void setOnGoodsSelectListener(GoodsSelectListener goodsSelectListener) {
        this.goodsSelectListener = goodsSelectListener;
    }

    public interface GoodsSelectListener {

        void goodsSelect(List<GoodsEntity> settlementData, List<GoodsEntity> deleteData);
    }


    public void setOnCheckAllListener(CheckAllListener checkAllListener) {
        this.checkAllListener = checkAllListener;
    }

    public interface CheckAllListener {
        void checkAllChange(boolean isPopShow);
    }


}
